package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.ClueActivityRelation;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.bjpowernode.crm.workbench.service.ClueActivityRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : ClueActivityRelationController
 * @Author : 司猫子
 * @Date: 2021/1/10 21:19
 */
@Controller
public class ClueActivityRelationController {
    @Autowired
    private ClueActivityRelationService clueActivityRelationService;
    @Autowired
    private ActivityService activityService;

    @RequestMapping("/workbench/clue/saveUnbundActivity.do")
    @ResponseBody
    public Object saveUnbundActivity(ClueActivityRelation clueActivityRelation) {
        ReturnObject returnObject = new ReturnObject();
        int i = clueActivityRelationService.deleteClueActivityRelationByClueIdActivityId(clueActivityRelation);
        if (i == 0) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("解除关联失败,请重新操作");
        } else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }
        return returnObject;
    }
    @RequestMapping("/workbench/clue/searchActivity.do")
    @ResponseBody
    public Object searchActivity(String activityName,String clueId) {
        ReturnObject returnObject = new ReturnObject();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("activityName",activityName);
        map.put("clueId",clueId);
        List<Activity> activities = activityService.selectActivityForDetailByNameClueId(map);
        return activities;
    }

    @RequestMapping("/workbench/clue/saveBundActivity.do")
    @ResponseBody
    public Object saveBundActivity(String[] activityId,String clueId) {
        ReturnObject returnObject = new ReturnObject();
        List<ClueActivityRelation> list = new LinkedList<ClueActivityRelation>();
        ClueActivityRelation clueActivityRelation = null;
        for (int i = 0; i < activityId.length; i++) {
            clueActivityRelation = new ClueActivityRelation();
            clueActivityRelation.setId(UUIDUtils.getUUID());
            clueActivityRelation.setClueId(clueId);
            clueActivityRelation.setActivityId(activityId[i]);
            list.add(clueActivityRelation);
        }
        int i = clueActivityRelationService.insertClueActivityRelationByList(list);
        if (i > 0){
            List<Activity> activityList = activityService.selectActivityForDetailByIds(activityId);
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setRetData(activityList);
        }else {
            returnObject.setMessage("批量关联失败,请稍后重试");
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
        }
        return returnObject;
    }
}
